'''
目标函数：
    min f(x)=x1^2+x2^2+x3^2+8
约束条件：
    x1^2-x2+x3^2>=0
    x1+x2^2+x3^3<=20
    -x1-x2^2+2=0
    x2+2*x3^2=3
    x1,x2,x3>=0

千辛万苦终于搞定这个非线性规划的解法
'''

from scipy.optimize import minimize
# 目标函数
fun = lambda x: x[0] ** 2 + x[1] ** 2 + x[2] ** 2 + 8
# 约束条件 分为eq和ineq eq默认=0 ineq默认>=0
cons = (
    {'type': 'eq', 'fun': lambda x: -x[0] - x[1] ** 2 + 2},
    {'type': 'eq', 'fun': lambda x: x[1] + 2 * x[2] ** 2 - 3},
    {'type': 'ineq', 'fun': lambda x: x[0] ** 2 - x[1] + x[2] ** 2},
    {'type': 'ineq', 'fun': lambda x: -x[0] - x[1] ** 2 - x[2] ** 3 + 20},
)
# 取值范围
bnds = (
    (0, None),
    (0, None),
    (0, None)
)
res = minimize(fun, (1,1,1), bounds=bnds, constraints=cons)
print(res)
# 结果很准确
print("zmin:",res.fun)
print("x:",res.x)
